package cn.jathlon.datastructure;

import java.nio.ByteBuffer;
import java.nio.IntBuffer;
/**
 * @author tuxiao.czz
 *
 */
public class IArrayUtils {
	
	/**
	 * int Array to ByteArray
	 * @param a
	 * @return
	 */
	public static byte [] intArray2ByteArray(int [] a){
		
        ByteBuffer byteBuffer = ByteBuffer.allocate(a.length * 4); 
        byteBuffer.asIntBuffer().put(a);
        return byteBuffer.array();
	}
	
	/**
	 * byteArray to intArray
	 * @param b
	 * @return
	 */
	public static int [] byteArray2intArray(byte [] b){
		
	    ByteBuffer byteBuffer2 = ByteBuffer.wrap(b);
	    IntBuffer ib = byteBuffer2.asIntBuffer();
	    int [] aa = new int[ib.limit()];
	    ib.get(aa);  ///
		return aa;
	}
	
	
	
	///////////
	public static void main(String[] args) {
		
		int [] a = {1112, 242, 23, 3244, 32, 68, 23312, 64, 129, 124, 9};
		
	    byte[] array = intArray2ByteArray(a);  ////
	    int [] aa = byteArray2intArray(array);   ///

	    for (int i = 0; i < aa.length; i++) {
			System.out.print(aa[i] + " ");
		}
	}
}
